﻿'========================================================================
'功能:郵件參數設定,
'作者:andy
'日期: 2011/3/2
'========================================================================
Imports System.Data
Partial Class WebForm_Manage_MailParamMaintain
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            If Not ChkProgramRight(1) Then '查詢
                gvBrowse.Visible = False
                btnInsert.Visible = False
            End If
            If Not ChkProgramRight(2) Then '新增
                btnInsert.Visible = False
            End If

            If ChkProgramRight(3) Then '編輯
                SetBtn(True)
            End If
            tbcMain.ActiveTabIndex = 0


        End If

    End Sub

    Private Sub SetBtn(ByVal Enable As Boolean)
        btnCancel.Visible = Enable
        btnSave.Visible = Enable
        ' plTools.Visible = Enable
    End Sub

    
    Protected Sub gvBrowse_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvBrowse.RowCommand
        If e.CommandName = "Detail" Then
            GetData(e.CommandArgument.ToString())
            tbcMain.ActiveTabIndex = 1

            If ChkProgramRight(3) Then '編輯
                SetBtn(True)
            Else
                SetBtn(False)
            End If

        End If
    End Sub
    Private Sub GetData(ByVal ParamID As String)
        Dim bbl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim dt As DataTable = bbl.GetMailParamByID(ParamID, hiDBName.Value)
        If dt.Rows.Count = 0 Then
            BlankData()
            Exit Sub
        End If
        lblParamID.Text = dt.Rows(0)("ParamID").ToString()
        txtParamDesc.Text = dt.Rows(0)("ParamDesc").ToString()
        txtMailSubject.Text = dt.Rows(0)("MailSubject").ToString()
        txtMailBody.Value = dt.Rows(0)("MailBody").ToString()
        If dt.Rows(0)("ModiName").ToString().Trim = "" Then
            lblModiDate.Text = dt.Rows(0)("CrtDate").ToString().Trim
            lblModiName.Text = dt.Rows(0)("CrtName").ToString().Trim
        Else
            lblModiDate.Text = dt.Rows(0)("ModiDate").ToString().Trim
            lblModiName.Text = dt.Rows(0)("ModiName").ToString().Trim
        End If
        txtReturnDays.Text = dt.Rows(0)("ReturnDays").ToString()
        txtResendDays.Text = dt.Rows(0)("ResendDays").ToString()
        txtResendTimes.Text = dt.Rows(0)("ResendTimes").ToString()
        txtMailBodyChn.Value = dt.Rows(0)("MailBodyChn").ToString()
        txtMailBodyEng.Value = dt.Rows(0)("MailBodyEng").ToString()
        txtMailsubjectSimplified.Text = dt.Rows(0)("MailSubjectChn").ToString()
        txtMailSubjectEng.Text = dt.Rows(0)("MailSubjectEng").ToString()



    End Sub
    Private Sub BlankData()
        lblParamID.Text = ""
        txtParamDesc.Text = ""
        txtMailSubject.Text = ""
        txtMailBody.Value = ""
        lblModiDate.Text = ""
        lblModiName.Text = ""
        txtReturnDays.Text = ""
        txtResendDays.Text = ""
        txtMailBodyChn.Value = ""
        txtMailBodyEng.Value = ""
        txtMailsubjectSimplified.Text = ""
        txtMailSubjectEng.Text = ""
    End Sub

    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        Dim bbl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim ParamID As String = bbl.InsertMailParam(hiUID.Value, hiDBName.Value)
        GetData(ParamID)
        gvBrowse.DataBind()
        SetBtn(True)

    End Sub

    Protected Sub btnModifyInfo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnModifyInfo.Click
        txtMailBody.Value = txtMailBody.Value.Replace("<p>", "").Replace("</p>", "") + HttpUtility.HtmlEncode("  ") + "|$|異動資料|$|"
    End Sub

    Protected Sub btnStatistics_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStatistics.Click
        txtMailBody.Value = txtMailBody.Value.Replace("<p>", "").Replace("</p>", "") + HttpUtility.HtmlEncode("  ") + "|$|統計|$|"

    End Sub

    Protected Sub btnLink_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLink.Click
        txtMailBody.Value = txtMailBody.Value.Replace("<p>", "").Replace("</p>", "") + HttpUtility.HtmlEncode("  ") + "|$|連結|$|"
    End Sub

    Protected Sub btnReturn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReturn.Click
        txtMailBody.Value = txtMailBody.Value.Replace("<p>", "").Replace("</p>", "") + HttpUtility.HtmlEncode("  ") + "|$|回報|$|"
    End Sub

    Protected Sub btnReturnAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReturnAll.Click
        txtMailBody.Value = txtMailBody.Value.Replace("<p>", "").Replace("</p>", "") + HttpUtility.HtmlEncode("  ") + "|$|回報全部|$|"
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        txtMailBody.Value = txtMailBody.Value.Replace("<p>", "").Replace("</p>", "") + HttpUtility.HtmlEncode("  ") + "|$|查詢|$|"
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        GetData(lblParamID.Text)

        If ChkProgramRight(3) Then '編輯
            SetBtn(True)
        Else
            SetBtn(False)
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If lblParamID.Text = "" Then
            Exit Sub
        End If
        If Not chkData() Then
            Exit Sub
        End If
        Dim bbl As AssetManage.BBL.MailParamBBL = New AssetManage.BBL.MailParamBBL()
        Dim rootDir As String = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/")
        Dim MailBody As String = txtMailBody.Value.Replace(rootDir + "/", "/AssetManage/").Replace("<p>", "").Replace("</p>", "").Replace("/AssetManage/", rootDir + "/")
        Dim chnMailBody As String = txtMailBodyChn.Value.Replace(rootDir + "/", "/AssetManage/").Replace("<p>", "").Replace("</p>", "").Replace("/AssetManage/", rootDir + "/")
        Dim EngMailBody As String = txtMailBodyEng.Value.Replace(rootDir + "/", "/AssetManage/").Replace("<p>", "").Replace("</p>", "").Replace("/AssetManage/", rootDir + "/")

        bbl.UpdateMailParam(lblParamID.Text, txtParamDesc.Text, Integer.Parse(txtReturnDays.Text), Integer.Parse(txtResendDays.Text), Integer.Parse(txtResendTimes.Text), txtMailSubject.Text, MailBody, hiUID.Value, txtMailsubjectSimplified.Text, txtMailSubjectEng.Text, chnMailBody, EngMailBody, hiDBName.Value)
        GetData(lblParamID.Text)
        gvBrowse.DataBind()

        If Not ChkProgramRight(3) Then '編輯
            SetBtn(False)
        Else
            SetBtn(True)
        End If

    End Sub
    Private Function chkData() As Boolean
        Try
            Integer.Parse(txtReturnDays.Text)
        Catch ex As Exception
            ShowMessage(GetLocalResourceObject("ErrIntReturnDays"))
            Return False
        End Try

        Try
            Integer.Parse(txtResendDays.Text)
        Catch ex As Exception
            ShowMessage(GetLocalResourceObject("ErrIntResendDays"))
            Return False
        End Try
        Try
            Integer.Parse(txtResendTimes.Text)
        Catch ex As Exception
            ShowMessage(GetLocalResourceObject("ErrIntResendTimes"))
            Return False
        End Try

        If txtMailBody.Value.Trim.Replace("<p>", "").Replace("</p>", "") = "" Then
            ShowMessage(GetLocalResourceObject("errMailBody"))
            Return False
        End If
        If txtMailSubject.Text.Trim = "" Then
            ShowMessage(GetLocalResourceObject("errMailSubject"))
            Return False
        End If
        If txtParamDesc.Text.Trim = "" Then
            ShowMessage(GetLocalResourceObject("errParamDesc"))
            Return False
        End If
        If txtMailBodyChn.Value = "" Then
            ShowMessage(GetLocalResourceObject("errMailBodyChn"))
            Return False
        End If
        If txtMailBodyEng.Value.Trim = "" Then
            ShowMessage(GetLocalResourceObject("errMailBodyEng"))
            Return False
        End If
        If txtMailsubjectSimplified.Text.Trim = "" Then
            ShowMessage(GetLocalResourceObject("errMailSubjectChn"))
            Return False
        End If
        If txtMailSubjectEng.Text.Trim = "" Then
            ShowMessage(GetLocalResourceObject("errMailSubjectEng"))
            Return False
        End If



        Return True

    End Function

 
End Class
